home *** CD-ROM | disk | FTP | other *** search
- /* Objective_C Implementation of the Fruit Class: Fruit.m */
-
- #import "Fruit.h"
-
- @implementation Fruit
-
- /* Initialize a new fruit. */
-
- #define RED_TAG 0
- #define GREEN_TAG 1
- #define YELLOW_TAG 2
- #define RED 0.0 //black
- #define GREEN 0.4 //a dark gray
- #define YELLOW 0.7 //a very light gray
-
- /* Intialize display to be showing correct current values. */
- -appDidInit:sender
- {
- [self displayValues:self];
- return self;
- }
-
- - initFrame:(const NXRect *)frameRect
- {
- [super initFrame:frameRect];
- [self setDiameter:1]; // init the diameter
- [self setColor:"red"]; // init the color
- return self; // return the new instance
- }
-
- /* Action method for setting the color of the fruit. */
- -takeColorFrom:sender
- {
- switch( [sender selectedTag]){
- case RED_TAG: [self setColor:"red"];
- break;
- case GREEN_TAG: [self setColor:"green"];
- break;
- case YELLOW_TAG: [self setColor:"yellow"];
- break;
- default: break;
- }
- return self;
- }
-
- /* Set the color of the fruit. */
- -setColor:(const char*)aColor
- {
- color = aColor;
- [self display];
- return self;
- }
-
- /* Return the fruit's color. */
- - (const char*)color
- {
- return color;
- }
-
- /* Action method for setting the diameter of the fruit. */
- -takeDiameterFrom:sender
- {
- [self setDiameter:[sender intValue]];
- return self;
- }
-
- /* Set the fruit's diameter. */
- -setDiameter: (int) aSize
- {
- diameter = aSize;
- [self display];
- return self;
- }
-
- /* Return the fruit's diameter. */
- - (int) diameter
- {
- return diameter;
- }
-
- /* Tell the fruit to increase its diameter. */
- - grow:sender
- {
- [self setDiameter:([self diameter] + 1)];
- return self;
- }
-
- - displayValues:sender
- {
- [colorDisplay setStringValue:color];
- [diameterDisplay setIntValue:diameter];
- return self;
- }
-
- /* This view only knows how to draw in three "colors". */
- - drawSelf:(const NXRect*)r :(int)c
- {
- float side;
-
- PSsetgray(NX_LTGRAY);
- NXRectFill(&bounds); //"erase" to gray
-
- /* Set "color" according to that selected. */
- if(strcmp(color,"red")==0) PSsetgray(RED);
- else if(strcmp(color,"green")==0) PSsetgray(GREEN);
- else if(strcmp(color,"yellow")==0) PSsetgray(YELLOW);
- else PSsetgray(RED); //default color
-
- side = [self diameter];
-
- /* Draw the fruit. */
- PSnewpath();
- PSmoveto(bounds.size.width/2.0-side/2.0, bounds.size.height/2.0-side/2.0);
- PSrlineto(0.0, side);
- PSrlineto(side,0.0);
- PSrlineto(0.0,-side);
- PSclosepath();
-
- PSfill(); //fill the defined path
- return self;
- }
-
- @end
-